home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreosl / moreappleevents / moreaeobjects.c next >
Encoding:
Text File  |  2000-06-23  |  15.7 KB  |  476 lines

  1. /*
  2.     File:        MoreAEObjects.c
  3.  
  4.     Contains:    Functions to help you when you are working with Apple event objects.
  5.  
  6.     Written by:    George Warner
  7.  
  8.     Copyright:    Copyright (c) 2000 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <1>      3/9/00    GW      Intergrating AppleEvent Helper code. First Check In
  21. */
  22.  
  23. //*******************************************************************************
  24. //    Conditionals to setup the build environment the way we like it.
  25. #include "MoreSetup.h"
  26. //**********    Universal Headers        ****************************************
  27. #include <AERegistry.h>
  28. #include <AEObjects.h>
  29. #include <AEPackObject.h>
  30. #include <Aliases.h>
  31. #include <FinderRegistry.h>
  32. #include <Icons.h>
  33. #include <Processes.h>
  34. //**********    Project Headers            ****************************************
  35. #include "MoreAppleEvents.h"
  36. #include "MoreAEObjects.h"
  37. //**********    Private Prototypes        ****************************************
  38. /*
  39.     Used by MoreFECreateIconFamilyRecord, passed to ForEachIconDo as the IconAction
  40.     function. Puts each icon in an icon suite into the descriptor record passed
  41.     in the myDataPtr parameter.
  42. */
  43.  
  44. static    pascal    OSErr    MyIconAction( ResType pIconType,
  45.                               const Handle *pIconHdl,
  46.                               void *pDataPtr);
  47. /********************************************************************************
  48.     Add a parameter of type typeAlias to an AERecord (or AppleEvent) using the provided FSSpec.
  49.  
  50.     pFSSpec            input:    Pointer to the FSSpec to use.
  51.     pKeyword        input:    The key for the data to be added to the record.
  52.     pAERecord        input:    Pointer to the record (or event) to add the data to.
  53.     
  54.     RESULT CODES
  55.     ____________
  56.     noErr               0    No error    
  57.     paramErr         -50    The value of target or alias parameter, or of
  58.                             both, is NIL, or the alias record is corrupt
  59.     memFullErr        -108    Not enough room in heap zone    
  60. */
  61. pascal    OSErr    MoreAEOAddAliasParameterFromFSSpec(const FSSpecPtr pFSSpec,
  62.                                                 const DescType pKeyword,
  63.                                                 AERecord *pAERecord )
  64. {
  65.     OSErr            anErr = noErr;
  66.     AliasHandle        aliasHandle;
  67.     
  68.     anErr = NewAlias( nil, pFSSpec, &aliasHandle);
  69.     if ( anErr == noErr  &&  aliasHandle == nil )
  70.     {
  71.         anErr = paramErr;
  72.     }
  73.     
  74.     if ( anErr == noErr )
  75.     {
  76.         char    handleState;
  77.         
  78.         handleState = HGetState( (Handle)aliasHandle );
  79.         HLock( (Handle)aliasHandle );
  80.         
  81.         anErr = AEPutParamPtr( pAERecord, pKeyword, typeAlias,
  82.                                *aliasHandle, (*aliasHandle)->aliasSize);
  83.         
  84.         HSetState( (Handle)aliasHandle, handleState );
  85.         DisposeHandle( (Handle)aliasHandle );
  86.     }
  87.         
  88.     return anErr;
  89. }//end AddAliasParameterFromFSS
  90. /********************************************************************************
  91.     Create and return an AEDesc of type typeAlias using the provided FSSpec.
  92.  
  93.     pFSSpec            input:    Pointer to the FSSpec to use.
  94.     pAliasAEDesc    input:    Pointer to null AEDesc.
  95.                     output:    an AEDesc of type typeAlias.
  96.     
  97.     RESULT CODES
  98.     ____________
  99.     noErr               0    No error    
  100.     paramErr         -50    The value of target or alias parameter, or of
  101.                             both, is NIL, or the alias record is corrupt
  102.     memFullErr        -108    Not enough room in heap zone    
  103. */
  104. pascal    OSErr    MoreAEOCreateAliasDescFromFSSpec( const FSSpecPtr pFSSpec,
  105.                                                   AEDesc *pAliasAEDesc )
  106. {
  107.     OSErr            anErr = noErr;
  108.     AliasHandle        aliasHandle;
  109.     
  110.     anErr = NewAlias( nil, pFSSpec, &aliasHandle);
  111.     if ( anErr == noErr  &&  aliasHandle == nil )
  112.     {
  113.         anErr = paramErr;
  114.     }
  115.  
  116.     if ( anErr == noErr )
  117.     {
  118.         anErr = MoreAEOCreateAliasDesc( aliasHandle, pAliasAEDesc );
  119.         DisposeHandle( (Handle)aliasHandle );
  120.     }
  121.         
  122.     return anErr;
  123. }//end MakeAliasObject
  124. /********************************************************************************
  125.     Create and return an AEDesc of type typeAlias using the provided 
  126.     alias record.
  127.  
  128.     pAliasHdl        input:    Handle to an alias record.
  129.     pAliasAEDesc    input:    Pointer to null AEDesc.
  130.                     output:    an AEDesc of type typeAlias.
  131.     
  132.     RESULT CODES
  133.     ____________
  134.     noErr               0    No error    
  135.     memFullErr        -108    Not enough room in heap zone    
  136. */
  137. pascal    OSErr    MoreAEOCreateAliasDesc( const AliasHandle pAliasHdl,
  138.                                         AEDesc *pAliasAEDesc )
  139. {
  140.     OSErr    anErr = noErr;
  141.     
  142.     char    handleState = HGetState( (Handle)pAliasHdl );
  143.     HLock( (Handle)pAliasHdl );
  144.     
  145.     anErr = AECreateDesc( typeAlias, *pAliasHdl, GetHandleSize( (Handle)pAliasHdl ), pAliasAEDesc );
  146.     
  147.     HSetState( (Handle)pAliasHdl, handleState );
  148.     
  149.     return anErr;
  150. }//end MakeAliasObject
  151. /********************************************************************************
  152.     Given an FSSpec, return an object descriptor containing an alias,
  153.     contained by containerObj.
  154.     
  155.     pFSSpec                input:    Pointer to the FSSpec to use.
  156.     pContainerAEDesc    input:    Pointer to container object for object being created.
  157.     pAliasObjectAEDesc        input:    Pointer to null AEDesc.
  158.                         output:    an alias object.
  159.     
  160.     RESULT CODES
  161.     ____________
  162.     noErr                    0    No error    
  163.     paramErr              -50    The value of target or alias parameter, or of
  164.                                 both, is NIL, or the alias record is corrupt
  165.     memFullErr             -108    Not enough room in heap zone    
  166.     errAECoercionFail     -1700    Data could not be coerced to the requested 
  167.                                 Apple event data type    
  168.     errAEWrongDataType    -1703    Wrong Apple event data type    
  169.     errAENotAEDesc        -1704    Not a valid descriptor record    
  170.     errAEBadListItem    -1705    Operation involving a list item failed    
  171. */
  172. pascal    OSErr    MoreAEOCreateAliasObjectFromFSSpec( const FSSpecPtr pFSSpec,
  173.                                                     AEDesc *pContainerAEDesc,
  174.                                                     AEDesc *pAliasObjectAEDesc )
  175. {
  176.     OSErr        anErr = noErr;
  177.     AEDesc        aliasDesc = {typeNull, nil};
  178.  
  179.     anErr = MoreAEOCreateAliasDescFromFSSpec( pFSSpec, &aliasDesc );
  180.     if ( anErr == noErr )
  181.     {
  182.         anErr = CreateObjSpecifier( typeAlias, pContainerAEDesc, formAbsolutePosition,
  183.                                     &aliasDesc, false, pAliasObjectAEDesc );
  184.         AEDisposeDesc( &aliasDesc );
  185.     }
  186.     
  187.     return anErr;
  188. }//end MakeAliasObject
  189. /********************************************************************************
  190.     Given an AliasHandle, return an object descriptor containing an alias,
  191.     contained by containerObj.
  192.     
  193.     pAliasHdl        input:    Handle to an alias record.
  194.     pContainerAEDesc    input:    Pointer to container object for object being created.
  195.     pAliasObjectAEDesc    input:    Pointer to null AEDesc.
  196.                     output:    an alias object.
  197.     
  198.     RESULT CODES
  199.     ____________
  200.     noErr                    0    No error    
  201.     paramErr              -50    Error in parameter list
  202.     memFullErr             -108    Not enough room in heap zone    
  203.     errAECoercionFail     -1700    Data could not be coerced to the requested 
  204.                                 Apple event data type    
  205.     errAEWrongDataType    -1703    Wrong Apple event data type    
  206.     errAENotAEDesc        -1704    Not a valid descriptor record    
  207.     errAEBadListItem    -1705    Operation involving a list item failed    
  208. */
  209. pascal    OSErr    MoreAEOCreateAliasObject( const AliasHandle pAliasHdl,
  210.                                           AEDesc *pContainerAEDesc,
  211.                                           AEDesc *pAliasObjectAEDesc )
  212. {
  213.     OSErr    anErr = noErr;
  214.     AEDesc    aliasDesc;
  215.  
  216.     anErr = MoreAEOCreateAliasDesc( pAliasHdl, &aliasDesc );
  217.     if ( anErr == noErr )
  218.     {
  219.         anErr = CreateObjSpecifier( typeAlias, pContainerAEDesc, formAbsolutePosition,
  220.                                     &aliasDesc, false, pAliasObjectAEDesc );
  221.         AEDisposeDesc( &aliasDesc );
  222.     }
  223.     
  224.     return anErr;
  225. }//end MakeAliasObject
  226. /********************************************************************************
  227.     Given a property type, create an new object descriptor for that property,
  228.     contained by containerObj.
  229.     
  230.     pPropertyType        input:    Property type to use for object.
  231.     pContainerAEDesc    input:    Pointer to container object for object being created.
  232.     propertyObjPtr    input:    Pointer to null AEDesc.
  233.                     output:    A property object.
  234.     
  235.     RESULT CODES
  236.     ____________
  237.     noErr                    0    No error    
  238.     paramErr              -50    Error in parameter list
  239.     memFullErr             -108    Not enough room in heap zone    
  240.     errAECoercionFail     -1700    Data could not be coerced to the requested 
  241.                                 Apple event data type    
  242.     errAEWrongDataType    -1703    Wrong Apple event data type    
  243.     errAENotAEDesc        -1704    Not a valid descriptor record    
  244.     errAEBadListItem    -1705    Operation involving a list item failed    
  245. */
  246. pascal    OSErr    MoreAEOCreatePropertyObject( const DescType pPropertyType,
  247.                                              AEDesc *pContainerAEDesc,
  248.                                              AEDesc *propertyObjPtr )
  249. {
  250.     OSErr    anErr = noErr;
  251.     AEDesc    propDesc;
  252.     
  253.     anErr = AECreateDesc( typeType, &pPropertyType, sizeof( pPropertyType ), &propDesc );
  254.     if ( anErr == noErr )
  255.     {
  256.         anErr = CreateObjSpecifier( cProperty, pContainerAEDesc, formPropertyID,
  257.                                     &propDesc, false, propertyObjPtr );
  258.         AEDisposeDesc( &propDesc );
  259.     }
  260.     
  261.     return anErr;
  262. }//end MakePropertyObject
  263. /********************************************************************************
  264.     Given a ProcessSerialNumber, create an new object descriptor for the PSN,
  265.     contained by containerObj.
  266.     
  267.     pPSN            input:    ProcessSerialNumber to use for object.
  268.     pContainerAEDesc    input:    Pointer to container object for object being created.
  269.     pPSNObjDesc        input:    Pointer to null AEDesc.
  270.                     output:    A ProcessSerialNumber object.
  271.     
  272.     RESULT CODES
  273.     ____________
  274.     noErr                    0    No error    
  275.     paramErr              -50    Error in parameter list
  276.     memFullErr             -108    Not enough room in heap zone    
  277.     errAECoercionFail     -1700    Data could not be coerced to the requested 
  278.                                 Apple event data type    
  279.     errAEWrongDataType    -1703    Wrong Apple event data type    
  280.     errAENotAEDesc        -1704    Not a valid descriptor record    
  281.     errAEBadListItem    -1705    Operation involving a list item failed    
  282. */
  283. pascal    OSErr    MoreAEOCreateProcessObject( const ProcessSerialNumber *pPSN,
  284.                                             AEDesc *pContainerAEDesc,
  285.                                             AEDesc *pPSNObjDesc )
  286. {
  287.     OSErr    anErr = noErr;
  288.     AEDesc    psnDesc;
  289.     
  290.     anErr = AECreateDesc( typeProcessSerialNumber, pPSN, sizeof( ProcessSerialNumber ), &psnDesc );
  291.     if ( anErr == noErr )
  292.     {
  293.         anErr = CreateObjSpecifier( cProperty, pContainerAEDesc, formPropertyID,
  294.                                     &psnDesc, false, pPSNObjDesc );
  295.         AEDisposeDesc( &psnDesc );
  296.     }
  297.     
  298.     return anErr;
  299. }//end MakePropertyObject
  300. /********************************************************************************
  301.     Given selection type, create an new object descriptor for a selection,
  302.     contained by containerObj.
  303.     
  304.     pSelectionAEDesc    input:    Selection type to use for object.
  305.     pContainerAEDesc    input:    Pointer to container object for object being created.
  306.     pSelectionObject        input:    Pointer to null AEDesc.
  307.                         output:    A property object.
  308.     
  309.     RESULT CODES
  310.     ____________
  311.     noErr                    0    No error    
  312.     paramErr              -50    Error in parameter list
  313.     memFullErr             -108    Not enough room in heap zone    
  314.     errAECoercionFail     -1700    Data could not be coerced to the requested 
  315.                                 Apple event data type    
  316.     errAEWrongDataType    -1703    Wrong Apple event data type    
  317.     errAENotAEDesc        -1704    Not a valid descriptor record    
  318.     errAEBadListItem    -1705    Operation involving a list item failed    
  319. */
  320. pascal    OSErr    MoreAEOCreateSelectionObject( const DescType pSelectionAEDesc,
  321.                                               AEDesc *pContainerAEDesc,
  322.                                               AEDesc *pSelectionObject )
  323. {
  324.     OSErr    anErr = noErr;
  325.     
  326.     AEDesc    selectionDesc = {typeNull, nil};
  327.     
  328.     anErr = AECreateDesc( typeAbsoluteOrdinal, &pSelectionAEDesc, sizeof( pSelectionAEDesc ), &selectionDesc );
  329.     if ( anErr == noErr )
  330.     {
  331.         anErr = CreateObjSpecifier( cObject, pContainerAEDesc, formAbsolutePosition,
  332.                                     &selectionDesc, false, pSelectionObject );
  333.         AEDisposeDesc( &selectionDesc );
  334.     }            
  335.     return anErr;
  336. }
  337. /********************************************************************************
  338.     Make position list (a list containing two longs representin the x and y values
  339.     for the position of a Finder item).
  340.     
  341.     pPosition            input:    A point specifying the position.
  342.     pPositionAEList        input:    Pointer to an AEList (contents will be lost, but not disposed).
  343.                         output:    A new AEList containing the x & y values for the position.
  344.     
  345.     Result Codes
  346.     ____________
  347.     noErr                    0    No error    
  348.     memFullErr             -108    Not enough room in heap zone    
  349. */
  350. pascal    OSErr    MoreAEOCreatePositionList( const Point pPosition,
  351.                                      AEDescList * pPositionAEList )
  352. {
  353.     OSErr    anErr = noErr;
  354.     
  355.     anErr = AECreateList( nil, 0, false, pPositionAEList );
  356.     if ( anErr == noErr )
  357.     {
  358.         anErr = AEPutPtr( pPositionAEList, 0, typeInteger, &(pPosition.h), sizeof( short ) );
  359.         if ( anErr == noErr )
  360.         {
  361.             anErr = AEPutPtr( pPositionAEList, 0, typeInteger, &(pPosition.v), sizeof( short ) );
  362.         }
  363.     }
  364.     
  365.     return anErr;
  366. }//end MoreAEOCreatePositionList
  367.  
  368. //********************************************************************************
  369. // A simple wrapper around CreateObjSpecifier which creates
  370. // an object specifier using formUniqueID and the unique ID
  371. // in pKeyData.
  372. pascal OSStatus MoreAEOCreateObjSpecifierFormUniqueID(DescType pDesiredClass, const AEDesc *pContainer, 
  373.                                                 SInt32 pKeyData, Boolean pDisposeInputs, 
  374.                                                 AEDesc *pObjSpecifier)
  375. {
  376.     OSStatus err;
  377.     AEDesc   keyDesc;
  378.  
  379.     MoreAssertQ(pContainer != nil);
  380.     MoreAssertQ(pObjSpecifier != nil);
  381.  
  382.     MoreAENullDesc(&keyDesc);
  383.     
  384.     err = AECreateDesc(typeLongInteger, &pKeyData, sizeof(SInt32), &keyDesc);
  385.     if (err == noErr) {
  386.         err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formUniqueID, &keyDesc, pDisposeInputs, pObjSpecifier);
  387.     }
  388.     
  389.     MoreAEDisposeDesc(&keyDesc);
  390.     
  391.     return err;
  392. }
  393.  
  394. //********************************************************************************
  395. // A simple wrapper around CreateObjSpecifier which creates
  396. // an object specifier using formAbsolutePosition, a key of
  397. // typeLongInteger (rather than typeAbsoluteOrdinal) and the
  398. // position index in pKeyData.
  399. pascal OSStatus MoreAEOCreateObjSpecifierFormAbsPos(DescType pDesiredClass, const AEDesc *pContainer, 
  400.                                             SInt32 pKeyData, SInt32 pDisposeInputs, 
  401.                                             AEDesc *pObjSpecifier)
  402. {
  403.     OSStatus err;
  404.     AEDesc   keyDesc;
  405.  
  406.     MoreAssertQ(pContainer != nil);
  407.     MoreAssertQ(pObjSpecifier != nil);
  408.  
  409.     MoreAENullDesc(&keyDesc);
  410.     
  411.     err = AECreateDesc(typeLongInteger, &pKeyData, sizeof(SInt32), &keyDesc);
  412.     if (err == noErr) {
  413.         err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formAbsolutePosition, &keyDesc, pDisposeInputs, pObjSpecifier);
  414.     }
  415.     
  416.     MoreAEDisposeDesc(&keyDesc);
  417.     
  418.     return err;
  419. }
  420.  
  421. //********************************************************************************
  422. // A simple wrapper around CreateObjSpecifier which creates
  423. // an object specifier using formName and the name in pKeyData.
  424. pascal OSStatus MoreAEOCreateObjSpecifierFormName(DescType pDesiredClass, const AEDesc *pContainer, 
  425.                                             ConstStr255Param pKeyData, Boolean pDisposeInputs, 
  426.                                             AEDesc *pObjSpecifier)
  427. {
  428.     OSStatus err;
  429.     AEDesc   keyDesc;
  430.  
  431.     MoreAssertQ(pContainer != nil);
  432.     MoreAssertQ(pKeyData != nil);
  433.     MoreAssertQ(pObjSpecifier != nil);
  434.     
  435.     MoreAENullDesc(&keyDesc);
  436.     
  437.     err = AECreateDesc(typeText, &pKeyData[1], pKeyData[0], &keyDesc);
  438.     if (err == noErr) {
  439.         err = CreateObjSpecifier(pDesiredClass, (AEDesc *) pContainer, formName, &keyDesc, pDisposeInputs, pObjSpecifier);
  440.     }
  441.     MoreAEDisposeDesc(&keyDesc);
  442.     return err;
  443. }
  444.  
  445. //********************************************************************************
  446. // *******************************************
  447. // *****       AddTwoIntListToEvent      *****
  448. // *******************************************
  449. //    Creates an AEList containing 2 integers, and then adds the list to an Apple event.
  450.  
  451. /*
  452. OSErr AddTwoIntListToEvent( AppleEvent *pAppleEvent, long param1, long param2 )
  453. {
  454.     OSErr    anErr;
  455.  
  456.     AEDescList    paramList;
  457.     anErr = AECreateList( nil, 0, false, ¶mList );
  458.     if ( anErr == noErr )
  459.     {
  460.         anErr = AEPutPtr( ¶mList, 0, typeInteger, ¶m1, sizeof( param1 ) );
  461.         if ( anErr == noErr )
  462.         {
  463.             anErr = AEPutPtr( ¶mList, 0, typeInteger, ¶m2, sizeof( param2 ) );
  464.             if ( anErr == noErr )
  465.             {
  466.                 anErr = AEPutParamDesc( pAppleEvent, keyDirectObject, ¶mList );
  467.             }
  468.         }
  469.     }
  470.     
  471.     AEDisposeDesc( ¶mList );
  472.     return ( anErr );
  473. }// end AddTwoIntListToEvent
  474.  
  475. */
  476.